<script setup>
import router from "../../../../router/index.js";
import MyNav from "../../../../components/MyNav.vue";
import MyForm from "../../../../components/MyForm.vue";
import {RULE} from "../../../../const/index.js";
import {reactive, ref} from "vue";
import {updateApi} from "../../../../api/axios.js";

// 获取当前菜单记录
let menu = JSON.parse(sessionStorage.getItem('row'));
let menuPart = menu['title'].split(' / ');
menu['pTitle'] = menuPart[0];
menu['title'] = menuPart[1];

/* ==================== 页面常量 ==================== */

// 路径导航
const navItems = [
  {icon: 'User', label: '用户管理', url: '/Menu'},
  {icon: 'Flag', label: '菜单列表（一级菜单）', url: '/Menu'},
  {icon: 'Flag', label: '菜单列表（二级菜单）', url: '/SubMenu'},
  {icon: 'Edit', label: '修改菜单（二级菜单）'},
];

/* ==================== 修改表单 ==================== */

// 表单项 + 表单值 + 表单规则
let items = ref([
  {label: '父级', prop: 'pTitle', disabled: true, span: 12},
  {label: '名称', prop: 'title', required: true, span: 12},
  {label: '排序', prop: 'idx', required: true, type: 'number', span: 12},
  {label: '地址', prop: 'url', required: true, span: 12},
  {label: '图标', prop: 'icon', required: true, type: 'icon'},
  {label: '描述', prop: 'info', required: true, type: 'textarea'},
]);
let data = reactive(menu);
let rules = {
  title: RULE.TITLE,
  url: RULE.MENU_URL,
  icon: RULE.MENU_ICON,
  info: RULE.INFO
};

/**
 * 修改成功后调用
 *
 * 1. 路由到 SubMenu 页面
 */
function updateSuccess() {
  router.push('/SubMenu');
}

</script>

<template>
  <my-nav :items="navItems"/>
  <el-divider/>
  <el-card class="sub-menu-update-card" header="修改二级菜单信息">
    <my-form type="update"
             :items="items"
             :rules="rules"
             :args="{module: 'menu'}"
             :api="updateApi"
             :params="data"
             :callback="updateSuccess"/>
  </el-card>
</template>

<style scoped lang="scss">
.sub-menu-update-card {
  width: 70%; // 宽度
  margin: 20px auto 0; // 外边距
}
</style>